package cn.mengfly.testnrz.feature;

import com.google.common.util.concurrent.*;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class GuavaFutureDemo {

    public static final int SLEEP_GAP = 500;

    public static String getCurThreadName() {
        return Thread.currentThread().getName();
    }

    static class HotWaterJob implements Callable<Boolean> {

        @Override
        public Boolean call() throws Exception {
            try {
                System.out.println("洗好水壶");
                System.out.println("灌上凉水");
                System.out.println("放在火上");
                Thread.sleep(SLEEP_GAP);
                System.out.println("水开了");
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println(getCurThreadName() + "发生异常被中断");
                return false;
            }
            System.out.println(getCurThreadName() + "运行结束.");
            return true;
        }
    }

    static class WashJob implements Callable<Boolean> {
        @Override
        public Boolean call() throws Exception {
            try {
                System.out.println("洗茶壶");
                System.out.println("洗茶杯");
                System.out.println("拿茶叶");
                Thread.sleep(SLEEP_GAP);
                System.out.println("洗完了");
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println(getCurThreadName() + "发生异常被中断");
                return false;
            }
            System.out.println(getCurThreadName() + "运行结束.");
            return true;
        }
    }


    static class MainJob implements Runnable {
        boolean waterOk = false;
        boolean cupOk = false;
        int gap = SLEEP_GAP / 10;

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(gap);
                    System.out.println("读书中...");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (waterOk && cupOk) {
                    drinkTea();
                }
            }
        }

        public void drinkTea() {
            System.out.println("泡茶喝");
            this.waterOk = false;
            this.gap = SLEEP_GAP * 100;

        }
    }

    public static void main(String[] args) {
        final MainJob mainJob = new MainJob();
        Thread mainTread = new Thread(mainJob);
        mainTread.setName("主线程");
        mainTread.start();

        Callable<Boolean> hotJob = new HotWaterJob();
        Callable<Boolean> washJob = new WashJob();

        // 创建Java线程池
        ExecutorService jPoll = Executors.newFixedThreadPool(10);
        ListeningExecutorService pool = MoreExecutors.listeningDecorator(jPoll);

        ListenableFuture<Boolean> hotFuture = pool.submit(hotJob);

        Futures.addCallback(hotFuture, new FutureCallback<Boolean>() {
            @Override
            public void onSuccess(@Nullable Boolean result) {
                if (result) {
                    mainJob.waterOk = true;
                }
            }

            @Override
            public void onFailure(Throwable t) {
                System.out.println("烧水失败，没有茶喝了");
            }
        }, pool);

        ListenableFuture<Boolean> washFuture = pool.submit(washJob);
        Futures.addCallback(washFuture, new FutureCallback<Boolean>() {
            @Override
            public void onSuccess(@Nullable Boolean result) {
                if (result) {
                    mainJob.cupOk = true;
                }
            }

            @Override
            public void onFailure(Throwable t) {
                System.out.println("杯子洗不了，没有茶喝了");
            }
        }, pool);
    }

}
